package cn.wk.controller;

import cn.wk.AjaxResult;
import cn.wk.PageList;
import cn.wk.dto.BuyTicketDto;
import cn.wk.query.MoveticketBuyQuery;
import cn.wk.service.IMoveticketBuyService;
import cn.wk.domain.MoveticketBuy;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/moveticketBuy")
public class MoveticketBuyController {
    @Autowired
    public IMoveticketBuyService moveticketBuyService;


    @PostMapping("/buyticket/buy")
    public AjaxResult buyTicked(@RequestBody BuyTicketDto buyTicketDto) {
        String ticketCode = moveticketBuyService.buyTicked(buyTicketDto);
        return AjaxResult.me().setSuccess(true).setMessage("购买成功").setResultObj(ticketCode);
    }


    /**
     * 保存和修改公用的
     *
     * @param moveticketBuy 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody MoveticketBuy moveticketBuy) {
        try {
            if (moveticketBuy.getId() != null)
                moveticketBuyService.updateById(moveticketBuy);
            else
                moveticketBuyService.insert(moveticketBuy);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            moveticketBuyService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public MoveticketBuy get(@PathVariable("id") Long id) {
        return moveticketBuyService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<MoveticketBuy> list() {

        return moveticketBuyService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<MoveticketBuy> json(@RequestBody MoveticketBuyQuery query) {
        Page<MoveticketBuy> page = new Page<MoveticketBuy>(query.getPage(), query.getRows());
        page = moveticketBuyService.selectPage(page);
        return new PageList<MoveticketBuy>(page.getTotal(), page.getRecords());
    }
}
